package com.action;

import java.io.File;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.po.User;
import com.po.model.Excel;
import com.util.JdbcUtils;

public class WebAction extends ActionSupport {

	private File[] upload;// must corresponds to the JSP page name
	private String[] uploadFileName;// must be file name + "FileName"
	private HttpServletRequest request;
	private Excel excel;
	User user = new User();

	public File[] getUpload() {
		return upload;
	}

	public void setUpload(File[] upload) {
		this.upload = upload;
	}
	public String[] getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String[] uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public Excel getExcel() {
		return excel;
	}

	public void setExcel(Excel excel) {
		this.excel = excel;
	}
	
	public String upload() throws Exception {
		String realPath = ServletActionContext.getServletContext().getRealPath("/files");
		if (null != upload) {
			File saveDir = new File(realPath);
			if (!saveDir.exists()) saveDir.mkdirs();
			for (int i = 0; i < upload.length; i++) {
				File saveFile = new File(saveDir,uploadFileName[i]);
				FileUtils.copyFile(upload[i], saveFile);
			}
			ActionContext.getContext().put("message", "上传成功");
		}
		return "success";
	}
	
	public void exportExl() {
		String string = request.getParameter("json");
//		System.out.println(string);
    }
	
	public void login() {
		try {
			if ( !("").equals(user.getUserName()) && !("").equals(user.getUserPassword())) {
				JdbcUtils utils = new JdbcUtils(user.getUserName(),user.getUserPassword());
				
//				ActionContext.getContext().put("userName", user.getUserName());
			}
			else {
				ActionContext.getContext().put("message", "输入空值");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}